# filepath: d:\CompanyProject\Gitee\DBCompareX\backend\app\models\task_data_sync\execution.py
"""
数据同步执行模块入口，负责整合其他模块功能，提供对外接口
"""
import logging

from .task_execution import TaskExecutor
from .data_utils import DataUtils
from .db_connectors import DBConnector
from .data_comparison import DataComparison
from .export_service import ExportService
from .execution_log import ExecutionLogManager

# 配置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

class TaskDataSyncExecutionModel:
    """数据同步任务执行模型，整合其他模块功能，提供对外接口"""

    # 代理到TaskExecutor
    @classmethod
    async def execute_task(cls, task_id):
        """执行数据同步任务"""
        return await TaskExecutor.execute_task(task_id)

    # 代理到ExecutionLogManager
    @classmethod
    async def get_execution_log(cls, execution_log_id):
        """获取执行日志"""
        return await ExecutionLogManager.get_execution_log(execution_log_id)
    
    @classmethod
    async def update_execution_log(cls, execution_log_id, status, message):
        """更新执行日志状态"""
        return await ExecutionLogManager.update_execution_log(execution_log_id, status, message)
    
    @classmethod
    async def get_execution_logs_by_task(cls, task_id, limit=10):
        """获取任务的执行日志列表"""
        return await ExecutionLogManager.get_execution_logs_by_task(task_id, limit)
    
    @classmethod
    async def query_execution_logs(cls, task_id=None, batch_no=None, status=None, page=1, page_size=10):
        """
        查询执行日志记录
        
        Args:
            task_id: 任务ID (可选)
            batch_no: 批次号 (可选)
            status: 状态 (可选)
            page: 当前页码，从1开始
            page_size: 每页记录数
            
        Returns:
            执行日志列表和总记录数
        """
        return await ExecutionLogManager.query_execution_logs(task_id, batch_no, status, page, page_size)
    
    @classmethod
    async def get_task_names(cls):
        """获取所有任务名称列表，用于下拉选择"""
        return await ExecutionLogManager.get_task_names()
    
    # 代理到ExportService
    @classmethod
    async def query_contrast_results(cls, execution_log_id):
        """查询对比结果"""
        return await ExportService.query_contrast_results(execution_log_id)
    
    @classmethod
    async def get_export_data(cls, execution_log_id):
        """获取导出数据"""
        return await ExportService.get_export_data(execution_log_id)
    
    @classmethod
    async def get_paired_comparison_data(cls, execution_log_id, offset=0, limit=100):
        """获取配对的对比数据，支持分页"""
        return await ExportService.get_paired_comparison_data(execution_log_id, offset, limit)
    
    # 数据处理工具方法代理
    @classmethod
    def handle_special_types(cls, obj):
        """处理特殊类型"""
        return DataUtils.handle_special_types(obj)
    
    @classmethod
    def process_data_for_mongodb(cls, data):
        """处理数据对象，使其兼容MongoDB存储"""
        return DataUtils.process_data_for_mongodb(data)
    
    @classmethod
    def compare_values(cls, value1, value2):
        """对比两个值是否相同"""
        return DataUtils.compare_values(value1, value2)
    
    # 数据库连接方法代理
    @classmethod
    async def execute_query_with_retry(cls, db_info, sql, params=None):
        """带重试机制的数据库查询"""
        return await DBConnector.execute_query_with_retry(db_info, sql, params)
    
    @classmethod
    async def execute_query(cls, db_info, sql, params=None):
        """执行数据库查询"""
        return await DBConnector.execute_query(db_info, sql, params)
    
    @classmethod
    async def execute_fabric_sqlserver_query(cls, db_info, sql, params=None):
        """执行Microsoft Fabric Data Warehouse查询"""
        return await DBConnector.execute_fabric_sqlserver_query(db_info, sql, params)
    
    @classmethod
    async def execute_mysql_query(cls, db_info, sql, params=None):
        """执行MySQL查询"""
        return await DBConnector.execute_mysql_query(db_info, sql, params)
    
    @classmethod
    async def execute_sqlserver_query(cls, db_info, sql, params=None):
        """执行SQL Server查询"""
        return await DBConnector.execute_sqlserver_query(db_info, sql, params)